package com.yiren.dbaa.module.practice_set.carmodel.view.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.backstage.currency.bean.PointExBean;
import com.yiren.dbaa.R;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelCollectContract;
import com.yiren.dbaa.view.popup.CarPointAdjustPopup;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Describe 车辆模型点位采集适配器
 *
 * @author WRX
 * @date 2022/12/7
 */
public class CarModelPointAdapter extends BaseAdapter {

    private Activity activity;
    private List<PointExBean> carModelPoints;
    private CarModelCollectContract.Presenter presenter;
    private OnPointActionListener listener;
    /*** 调整车来那个点位的弹窗 */
    private CarPointAdjustPopup adjustPointPop;

    public CarModelPointAdapter(Activity activity, List<PointExBean> carModelPoints, CarModelCollectContract.Presenter presenter, OnPointActionListener listener) {
        this.activity = activity;
        this.carModelPoints = carModelPoints;
        this.presenter = presenter;
        this.listener = listener;
        adjustPointPop = new CarPointAdjustPopup(activity, 240, 128);
        adjustPointPop.setAnimationStyle(R.style.popupAnimation);
    }

    @Override
    public int getCount() {
        return carModelPoints.size();
    }

    @Override
    public Object getItem(int position) {
        return carModelPoints.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_car_model_point, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PointExBean point = carModelPoints.get(position);
        holder.tvAdjust.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
        holder.tvIndex.setText("" + position);
        if (null == point) {
            holder.tvCollect.setText("采集");
            holder.tvPointX.setText("X:");
            holder.tvPointY.setText("Y:");
        } else {
            holder.tvCollect.setText("校准");
            holder.tvPointX.setText("X:" + point.getX());
            holder.tvPointY.setText("Y:" + point.getY());
        }
        //调整
        holder.tvAdjust.setOnClickListener(v -> {
            if (presenter.isCanRecover(carModelPoints)) {
                adjustPointPop.show(v);
                adjustPointPop.setOnViewClickListener(new CarPointAdjustPopup.OnViewClickListener() {
                    @Override
                    public void onItemClick(int flag, float value) {
                        switch (flag) {
                            case 1:  //上
                                point.setY(point.getY() + value);
                                break;
                            case 2:  //左
                                point.setX(point.getX() - value);
                                break;
                            case 3:  //右
                                point.setX(point.getX() + value);
                                break;
                            case 4:  //下
                                point.setY(point.getY() - value);
                                break;
                        }
                        adjustPointPop.dismiss();
                        notifyDataSetChanged();
                        if (position == 1 || position == 13 || position == 25 || position == 27 || position == 29 || position == 31) {
                            listener.refreshMargin();
                        }
                    }
                });
            } else {
                MessageBox.show(activity, "请您在调整前先保存车型");
            }
        });
        //采集
        holder.tvCollect.setOnClickListener(v -> {
            listener.onPointCollect(position);
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_index)
        TextView tvIndex;
        @BindView(R.id.tv_point_x)
        TextView tvPointX;
        @BindView(R.id.tv_point_y)
        TextView tvPointY;
        @BindView(R.id.tv_adjust)
        TextView tvAdjust;
        @BindView(R.id.tv_collect)
        TextView tvCollect;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnPointActionListener {

        void refreshMargin();

        void onPointCollect(int position);
    }
}
